package com.sprite.utils.security;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 安全工具类
 * @author Jack
 * @see com.sprite.utils.security.UtilMessageDigest
 * @deprecated
 */
public final class UtilMD5 {
	private UtilMD5(){}

	private static final char[] HEX_CHARS = {'0','1','2','3','4','5','6','7','8','9','a','b', 'c', 'd','e', 'f'};
	/**
	 * MD5摘要计算
	 * @param str 文本
	 * @param charSet 文本字符集
	 * @return md5值
	 */
	public static final String digestByMD5(String str,String charSet) {
		MessageDigest messageDigest = null;    
		try {    
			messageDigest = MessageDigest.getInstance("MD5");    
			messageDigest.reset();   
			if(charSet==null){  
				messageDigest.update(str.getBytes());  
			}else{  
				messageDigest.update(str.getBytes(charSet));    
			}             
		} catch (NoSuchAlgorithmException e) {    
		} catch (UnsupportedEncodingException e) {    
		}    

		byte[] byteArray = messageDigest.digest();    
		StringBuffer md5StrBuff = new StringBuffer();
		int bs ;
		for(byte bt : byteArray){
			bs = bt;
			if(bs <0){
				bs+=256;
			}
			md5StrBuff.append(HEX_CHARS[bs>>4]);
			md5StrBuff.append(HEX_CHARS[bs&0x0F]);
		}
		return md5StrBuff.toString();
	}

}
